from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.mixins import CacheResponseMixin

from .serializers import AreaSerializer, SubsAreaSerializer
from .models import Area


# Create your views here.
class AreasViewSet(CacheResponseMixin, ReadOnlyModelViewSet):
    """省市区数据查询"""

    pagination_class = None  # 禁用分页,因为后面商品列表地方开启全部分页

    # 指定序列化器
    # serializer_class = AreaSerializer
    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer  # 列表视图时需要用的序列化器
        else:
            return SubsAreaSerializer  # 详情视图需要用的序列化器

    # 指定查询集
    # queryset = Area.objects.filter(parent=None)
    def get_queryset(self):
        if self.action == 'list':
            return Area.objects.filter(parent=None)  # 只返回省的数据
        else:
            return Area.objects.all()  # 返回所有数据



# Area.objects.get(id=130000)
